import { login } from '@/apis/user'
import { defineStore } from 'pinia'
import { ref } from 'vue'

export const useUserStore = defineStore(
  'user',
  () => {
    // 用户token
    const token = ref('')

    // 用户信息
    const userInfo: any = ref()

    // 设置用户信息
    const getUserInfo = async (loginForm: any) => {
      const res = await login(loginForm)
      userInfo.value = res

      return userInfo
    }

    // 退出登录
    const logout = () => {
      token.value = ''
      userInfo.value = ''
    }

    return {
      token,
      userInfo,
      getUserInfo,
      logout,
    }
  },
  {
    persist: true,
  },
)
